package tank;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import abstractBeanFactory.AbstractTankBeanFactory;
import abstractBeanFactory.BaseBullet;
import abstractBeanFactory.BaseExplode;
import abstractBeanFactory.BaseTank;
import abstractBeanFactory.RectBeanFactory.RectBeanFactory;
import abstractBeanFactory.defaultBeanFactory.DefaultBeanFactory;

public class TankFrame extends Frame{
	public static final int GAME_WIDTH =1080;
	public static final int GAME_HEIGHT = 960 ;
	Tank myTank = new Tank(200,400,Dir.DOWN,Group.GOOD,this);
	public List<BaseBullet> bullets = new ArrayList<>();
	public List<BaseTank> tanks = new ArrayList<>(); 
	public List<BaseExplode> explodes = new ArrayList<BaseExplode>(); 
//	public AbstractTankBeanFactory abstractTankBeanFactory = DefaultBeanFactory.getInstance(); 
	public AbstractTankBeanFactory abstractTankBeanFactory = new RectBeanFactory();
	public TankFrame() {
		setSize(GAME_WIDTH,GAME_HEIGHT);
		setResizable(false);
		setTitle("tank war");
		setVisible(true);
		addKeyListener(new MyKeyListener());
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
	}
	
	//解决闪烁问题
	Image offScreenImage = null;
	@Override
	public void update(Graphics g) {
		if(offScreenImage ==null) {
			offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGHT);
		}
		Graphics gOffScreen = offScreenImage.getGraphics();
		Color c= gOffScreen.getColor();
		gOffScreen.setColor(Color.BLACK);
		gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_WIDTH);
		paint(gOffScreen);
		g.drawImage(offScreenImage, 0, 0, null);
	}
	
	@Override
	public void paint(Graphics g) {
		Color color = g.getColor();
		g.setColor(Color.WHITE);
		g.drawString("子弹的数量"+bullets.size(), 10, 60);
		g.drawString("敌人坦克的数量"+tanks.size(), 10, 80);
		g.drawString("爆炸的数量"+explodes.size(), 10, 100);
		g.setColor(color);
//		if(myTank.isLiving()) {
			myTank.paint(g);
//		}
//		b.paint(g);
		for(int i=0;i<bullets.size();i++) {
			bullets.get(i).paint(g);
		}
		
//		for(Iterator<Bullet> iterator = bullets.iterator();iterator.hasNext();) {
//			if(iterator.hasNext()) {
//				Bullet bullet = iterator.next();
//				if(!bullet.isLive()) {
//					iterator.remove();
//				}
//				bullet.paint(g); 
//			}
//		}
		for(int i=0;i<tanks.size();i++) {
			tanks.get(i).paint(g);
		}
		for(int i=0;i<explodes.size();i++) {
			explodes.get(i).paint(g);
		}
		
		//只是做了主战tank的炮弹和敌方坦克的碰撞测试
		for(int i=0;i<bullets.size();i++) {
			//敌人tank和主站tank的碰撞检测
//			bullets.get(i).collideWith(myTank);
			for(int j=0;j<tanks.size();j++) {
				bullets.get(i).collideWith(tanks.get(j));
			}
		}
		
	}
	
	class MyKeyListener extends KeyAdapter{
		boolean bl=false;
		boolean br=false;
		boolean bu=false;
		boolean bd=false;
		@Override
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			switch (key) {
			case KeyEvent.VK_LEFT:
				bl =true;
				break;
			case KeyEvent.VK_UP:
				bu =true;
				break;
			case KeyEvent.VK_RIGHT:
				br =true;
				break;
			case KeyEvent.VK_DOWN:
				bd =true;
				break;
			default:
				break;
			}
			serMainTankDir();
		}

		@Override
		public void keyReleased(KeyEvent e) {
			int key = e.getKeyCode();
			switch (key) {
			case KeyEvent.VK_LEFT:
				bl =false;
				break;
			case KeyEvent.VK_UP:
				bu =false;
				break;
			case KeyEvent.VK_RIGHT:
				br =false;
				break;
			case KeyEvent.VK_DOWN:
				bd =false;
				break;
			case KeyEvent.VK_CONTROL:
				myTank.fire();
			default:
				break;
			}
			serMainTankDir();
		}
		private void serMainTankDir() {
			if(!bl&&!bu&&!br&&!bd) {
				myTank.setMoving(false);
			}else {
				myTank.setMoving(true);
				if(bl) myTank.setDir(Dir.LEFT);
				if(bu) myTank.setDir(Dir.UP);
				if(br) myTank.setDir(Dir.RIGHT);
				if(bd) myTank.setDir(Dir.DOWN);
			}
		}
		
	}
}
